package com.xique.door.biz.service.impl;

import com.xique.door.bean.domain.Visitor;
import com.xique.door.bean.domain.VisitorDevice;
import com.xique.door.biz.service.IDoorDeviceCommandBizService;
import com.xique.door.constant.DoorDeviceConstant;
import com.xique.door.device.freeview.service.biz.FreeviewCommandBizService;
import com.xique.door.device.hq.bean.response.HqDownPersonCommandResponse;
import com.xique.door.device.hq.service.IHqDeviceCommandBizService;
import com.xique.door.service.IVisitorService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2021/9/23 14:03
 */
@Service
@Deprecated
public class DoorDeviceCommandBizServiceImpl implements IDoorDeviceCommandBizService {

    @Autowired
    private IHqDeviceCommandBizService hqDeviceCommandBizService;

    @Autowired
    private FreeviewCommandBizService freeviewCommandBizService;

    @Autowired
    private IVisitorService visitorService;

    @Override
    public boolean downloadSingletonVisitor(List<VisitorDevice> list) {

        Map<String, List<VisitorDevice>> map = list.stream().filter(item -> !item.getStatus().equals(DoorDeviceConstant.DeviceStatus.SUCCESS)).collect(Collectors.toMap(
                VisitorDevice::getModel,
                item -> new ArrayList<>(Arrays.asList(item)),
                (List<VisitorDevice> oldList, List<VisitorDevice> newList) -> {
                    oldList.addAll(newList);
                    return oldList;
                }));

        if (CollectionUtils.isNotEmpty(map.get(DoorDeviceConstant.DeviceModel.HQ))) {
            // TODO 海清设备下发
            List<VisitorDevice> hq = map.get(DoorDeviceConstant.DeviceModel.HQ);
            List<String> devSns = hq.stream().map(VisitorDevice::getDevSn).collect(Collectors.toList());
            hqDeviceCommandBizService.downVisitor(hq.get(0).getVisitorId(), devSns);
        }

        if (CollectionUtils.isNotEmpty(map.get(DoorDeviceConstant.DeviceModel.QST_32))) {
            // TODO 全视通32寸设备下发
            List<VisitorDevice> qst = map.get(DoorDeviceConstant.DeviceModel.QST_32);
            List<String> devSns = qst.stream().map(VisitorDevice::getDevSn).collect(Collectors.toList());
            Long visiorId = qst.get(0).getVisitorId();
            Visitor visitor = visitorService.selectVisitorById(visiorId);
            freeviewCommandBizService.downVisitorInfo(devSns, visitor);
        }

        if (CollectionUtils.isNotEmpty(map.get(DoorDeviceConstant.DeviceModel.QST_45))) {
            // TODO 全视通4.5寸设备下发
            List<VisitorDevice> qst = map.get(DoorDeviceConstant.DeviceModel.QST_45);
            List<String> devSns = qst.stream().map(VisitorDevice::getDevSn).collect(Collectors.toList());
            Long visiorId = qst.get(0).getVisitorId();
            Visitor visitor = visitorService.selectVisitorById(visiorId);
            freeviewCommandBizService.downVisitorInfo(devSns, visitor);
        }

        return true;
    }

    @Override
    public boolean delSingletonVisitor(List<VisitorDevice> list) {

        Map<String, List<VisitorDevice>> map = list.stream().collect(Collectors.toMap(
                VisitorDevice::getModel,
                item -> new ArrayList<>(Arrays.asList(item)),
                (List<VisitorDevice> oldList, List<VisitorDevice> newList) -> {
                    oldList.addAll(newList);
                    return oldList;
                }));

        if (CollectionUtils.isNotEmpty(map.get(DoorDeviceConstant.DeviceModel.HQ))) {
            // TODO 海清设备下发
            List<VisitorDevice> hq = map.get(DoorDeviceConstant.DeviceModel.HQ);
            List<String> devSns = hq.stream().map(VisitorDevice::getDevSn).collect(Collectors.toList());
            hqDeviceCommandBizService.delVisitor(hq.get(0).getVisitorId(), devSns);
        }

        if (CollectionUtils.isNotEmpty(map.get(DoorDeviceConstant.DeviceModel.QST_32))) {
            // TODO 全视通32寸设备下发
            List<VisitorDevice> qst = map.get(DoorDeviceConstant.DeviceModel.QST_32);
            List<String> devSns = qst.stream().map(VisitorDevice::getDevSn).collect(Collectors.toList());
            Long visiorId = qst.get(0).getVisitorId();
            Visitor visitor = visitorService.selectVisitorById(visiorId);
            freeviewCommandBizService.deleteVisitorInfo(devSns, visitor);
        }

        if (CollectionUtils.isNotEmpty(map.get(DoorDeviceConstant.DeviceModel.QST_45))) {
            // TODO 全视通4.5寸设备下发
            List<VisitorDevice> qst = map.get(DoorDeviceConstant.DeviceModel.QST_45);
            List<String> devSns = qst.stream().map(VisitorDevice::getDevSn).collect(Collectors.toList());
            Long visiorId = qst.get(0).getVisitorId();
            Visitor visitor = visitorService.selectVisitorById(visiorId);
            freeviewCommandBizService.deleteVisitorInfo(devSns, visitor);
        }

        return true;
    }
}
